﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CPPEI.Coolzon.UI
{
    public class ActionUrl
    {
        public string ActionType { get; set; }
        public string TargetFrame { get; set; }
        public string TargetScene { get; set; }
        /// <summary>
        /// 参数定义文本信息，更具次信息可以转换成实际的参数
        /// </summary>
        public Dictionary<string, string> ParameterString { get; set; }
        public string Width { get; set; }
        public string Height { get; set; }
        public string X { get; set; }
        public string Y { get; set; }

        /// <summary>
        /// replace://Frame=x;Scene=s1?p=1;Width=1;Height=1;X=1;Y=1;
        /// pop://Frame=x;Scene=s1?p=1;Width=1;Height=1;X=1;Y=1;
        /// </summary>
        /// <param name="urlString"></param>
        public ActionUrl(string urlString)
        {
            urlString = urlString.Trim();
            string[] strArr = urlString.Split(new string[] { "://" }, StringSplitOptions.RemoveEmptyEntries);
            ActionType = strArr[0].ToUpper();
            string[] segmentArr = strArr[1].Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
            for (int index = 0; index < segmentArr.Length; index++)
            {
                string segment = segmentArr[index];
                string[] kvArr = segment.Split(new char[] { '=' }, StringSplitOptions.RemoveEmptyEntries);
                string key = kvArr[0].ToUpper();
                if (key == "FRAME")
                {
                    TargetFrame = kvArr[1];
                }
                else if (key == "SCENE")
                {
                    ParseScene(segment);
                }
                else if (key == "WIDTH")
                {
                    Width = kvArr[1];
                }
                else if (key == "HEIGHT")
                {
                    Height = kvArr[1];
                }
                else if (key == "X")
                {
                    X = kvArr[1];
                }
                else if (key == "Y")
                {
                    Y = kvArr[1];
                }
            }
        }

        private void ParseScene(string sceneString)
        {
            this.ParameterString = new Dictionary<string, string>();

            string[] segmentArr = sceneString.Split(new char[] { '?' }, StringSplitOptions.RemoveEmptyEntries);

            TargetScene = segmentArr[0].Split(new char[] { '=' }, StringSplitOptions.RemoveEmptyEntries)[1];

            if (segmentArr.Length > 1)
            {
                string[] parameterArr = segmentArr[1].Split(new char[] { '&' }, StringSplitOptions.RemoveEmptyEntries);
                for (int index = 0; index < parameterArr.Length; index++)
                {
                    string[] kvArr = parameterArr[index].Split(new char[] { '=' }, StringSplitOptions.RemoveEmptyEntries);
                    if (kvArr.Length == 2)
                    {
                        this.ParameterString.Add(kvArr[0], kvArr[1]);
                    }
                }
            }
        }
    }
}
